package pers.yanfan.lock;

import java.util.concurrent.locks.Lock;
import org.jgroups.JChannel;
import org.jgroups.blocks.locking.LockService;

import com.gitee.yanfanvip.ClusterBuild;
import com.gitee.yanfanvip.util.IPHelp;

public class LockDemo {
	
	public static void main(String[] args) throws Exception {
		JChannel channel = new ClusterBuild("MESSAGE", 18500, IPHelp.getIPPorts(18500, 18600, 18700)).getChannel();
		LockService lockService = new LockService(channel);
		Lock lock = lockService.getLock("mylock");
		if(lock.tryLock()){
			try {
				System.out.println("locked");
				Thread.sleep(60000);
			} finally {
			    lock.unlock();
			}
		}
		System.out.println("exit");
		channel.close();
	}
}
